package it.antonio.ilpizze.db.index.btree;

import it.antonio.ilpizze.func.ComputeIterator;
import it.antonio.ilpizze.func.Pair;

public class BTreeBrowser<K extends Comparable<K>, V> extends ComputeIterator<Pair<K, V>> {
	
	private LeafNode<K, V> current;
	private Integer index;
	
	

	public BTreeBrowser(LeafNode<K, V> current, Integer index) {
		this.current = current;
		this.index = index;
	}

	@Override
	protected Pair<K, V> computeNext() {
		if(index < current.size()){
			Entry<K, V> toReturn = current.get(index);
			
			index++;
			return toPair(toReturn);
		} else {
			index = 0;
			current = current.next();
			if(current != null){
				return computeNext();	
			} else {
				return finished();
			}
			
		}
	}

	private Pair<K, V> toPair(Entry<K, V> toReturn) {
		return Pair.from(toReturn.key(), toReturn.value());
	}
	
}
